// import axios from 'axios';

import router from '@/router';

const isProduction = process.env.NODE_ENV === 'production';
let baseURL;
if (isProduction) {
  baseURL = 'http://8.135.2.160:13000';
} else {
  // baseURL = 'http://localhost:3000';
  baseURL = 'http://8.135.2.160:13000';
  // baseURL = 'http://8.135.2.160:3000';
}



const request = axios.create({
  baseURL,
  timeout: 30000,
  // headers: {'Content-Type': 'application/json;charset=utf-8'},// 设置传输内容的类型和编码
});

const protectedRequest = () => {
  const ans = axios.create({
    baseURL,
    timeout: 30000,
    // headers: {'Authorization': `Bearer ` + window.localStorage.getItem('token')},// 设置传输内容的类型和编码
  });
  ans.interceptors.request.use((config) => {
    // 在发送请求之前做些什么
    if (window.localStorage.getItem('token')) {
      config.headers['Authorization'] = `Bearer ` + window.localStorage.getItem('token');
    }
    return config;
  }, (error) => {
    // 对请求错误做些什么
    return Promise.reject(error);
  });

  ans.interceptors.response.use(
    response => {
      return response
    },
    error => {
      if (error.response) {
        switch (error.response.status) {
          case 401:
            router.replace({
              name: 'Login'
            })
        }
      }
      return Promise.reject(error.response)
    }
  );
  return ans;
}




export {
  request,
  protectedRequest
};